/*******************************************************************
* Copyright (c) 2010 BSUIR <romkisel@gmail.com>

* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:

* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
********************************************************************/

#include <QMenu>
#include <QGraphicsSceneContextMenuEvent>
#include "plight.h"
#include "optixscene.h"
#include "shadowmap.h"

PLight::PLight(ShadowMap *shadMap, qreal h, qreal size, QColor color): OptixItem(color)
{
    this->shadMap = shadMap;
    this->mH = h;
    this->mSize = size;

    gradient = new QRadialGradient(QPointF(0, 0), mSize, QPointF(0, 0));
    gradient->setColorAt(0, QColor(color.red(), color.green(), color.blue(), 130));
    gradient->setColorAt(1, QColor(color.red(), color.green(), color.blue(), 0));

    this->setFlag(QGraphicsItem::ItemIsMovable, true);
    this->setFlag(QGraphicsItem::ItemIsSelectable, true);
}

void PLight::drawLight()
{
    //draw light on the shadow map
    qreal imageW = shadMap->getMap()->width();
    qreal imageH = shadMap->getMap()->height();
    qreal x = pos().x();
    qreal y = pos().y();

    QPainter p;
    p.begin(shadMap->getMap());
    //substract the light from the shadow map
    p.setCompositionMode(QPainter::CompositionMode_Clear);
    p.setPen(Qt::NoPen);
    p.setBrush(Qt::black);
    p.drawEllipse(x+imageW/2 - mSize/2, y+imageH/2 - mSize/2, mSize, mSize);
    p.setCompositionMode(QPainter::CompositionMode_SourceOver);
    p.end();
}

qreal PLight::getH()
{
    return mH;
}

qreal PLight::getSize()
{
    return mSize;
}

void PLight::setH(qreal h)
{
    this->mH = h;
}

void PLight::setSize(qreal size)
{
    this->mSize = size;
}

void PLight::changeColor()
{
    gradient = new QRadialGradient(QPointF(0, 0), mSize, QPointF(0, 0));
    gradient->setColorAt(0, QColor(color.red(), color.green(), color.blue(), 130));
    gradient->setColorAt(1, QColor(color.red(), color.green(), color.blue(), 0));

    this->update();
}

QRectF PLight::boundingRect() const
{
    return QRectF(-15, -15, 30, 30);
}

void PLight::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter->setPen(Qt::NoPen);
    painter->setBrush(*gradient);
    painter->drawEllipse(-mSize/2, -mSize/2, mSize, mSize);

    if (isSelected())
    {
        painter->setPen(QPen(Qt::black, 1, Qt::DashLine));
        painter->setBrush(Qt::NoBrush);
        painter->drawRect(this->boundingRect());
    }

    painter->setPen(Qt::green);
    painter->setBrush(Qt::NoBrush);
    painter->drawEllipse(boundingRect().center(),
                         boundingRect().width()/2,
                         boundingRect().height()/2);
}

void PLight::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
    scene()->clearSelection();
    setSelected(true);

    OptixScene *sc = dynamic_cast<OptixScene *>(scene());
    if (sc)
    {
        sc->ContextMenu()->exec(event->screenPos());
    }
}
